package Service;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class CBFsocket {
    String server_ip="192.168.43.70";
    int server_port=12345;
    String cbf_response = "";
    String cbf_request = "";
    public String sendRequest(String cbf_request){
        this.cbf_request=cbf_request;
        ThreadTest t1=new ThreadTest();
        t1.start();
        try {
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return cbf_response;
    }
    class ThreadTest extends Thread {


        public void run(){
            try {
            Socket socket = new Socket(server_ip, server_port);
            //2.获取输出流，向服务器端发送信息
            OutputStream os = socket.getOutputStream();//字节输出流
            PrintWriter pw = new PrintWriter(os);//将输出流包装为打印流

            pw.write(cbf_request);
            pw.flush();
            socket.shutdownOutput();//关闭输出流

            InputStream is=null;
            InputStreamReader isr=null;
            BufferedReader br=null;
            is = socket.getInputStream();     //获取输入流
            isr = new InputStreamReader(is,"UTF-8");
            br = new BufferedReader(isr);
            String info = null;
            while((info=br.readLine())!=null){//循环读取客户端的信息
                cbf_response= cbf_response +info;
            }
            System.out.println("服务器发送过来的信息" + cbf_response);


        } catch (IOException e) {
            e.printStackTrace();
        }
        }
    }

}
